function [route, existflag] = hadlock(Maze, source, target)

  route = [];
  existflag = 1;
  visited = -2;

  Pstack = []; Nstack = [];

  % step 1
  d = 0; u = source;

  % step 2
  Maze(u(1),u(2)) = d; % incremental label?

  while (sum(u==target) ~= 2)
    [pos, neg] = neighbor(Maze, target, u);
    Nstack = [neg; Nstack];

    if isempty(pos) ~= 1
      v = pos(1,:);
      if isnan(Maze(v(1),v(2))) ~= 1
        continue;
      end
      u = v;
      Pstack = [pos(2:end,:); Pstack];

    else
      % step 3
      while (1)
        if isempty(Pstack) ~= 1
          v = Pstack(1,:); Pstack = Pstack(2:end,:);
          if isnan(Maze(v(1),v(2))) ~= 1
            continue;
          end
          u = v;
          break;
        else
          if isempty(Nstack)
            existflag = 0;
            break;
          end
          Pstack = Nstack;
          Nstack = [];
          d = d + 1;
        end
      end
    end

    if existflag == 0
      break;
    end

    % update u
    Maze(u(1),u(2)) = d;

  end

  if existflag
    route = hadlockretrace(Maze, source, target, d);
  end

end
